package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Order;
import cn.wolfcode.trip.base.domain.OrderItem;
import cn.wolfcode.trip.base.domain.ShopCarItem;
import cn.wolfcode.trip.base.domain.SignDetail;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.domain.Order;
import cn.wolfcode.trip.base.mapper.OrderItemMapper;
import cn.wolfcode.trip.base.mapper.OrderMapper;
import cn.wolfcode.trip.base.mapper.SignCalcMapper;
import cn.wolfcode.trip.base.mapper.SignDetailMapper;
import cn.wolfcode.trip.base.query.OrderQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IOrderService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PageInfo query(OrderQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Order> list = orderMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Order order) {
        orderMapper.updateByPrimaryKey(order);
    }

    @Override
    public void UpdateOrder(Order order) {
        orderMapper.UpdateOrder(order);
    }


    /**
     * 查询当前用户所有订单
     * @param userId
     */
    @Override
    public List<Order> getCurrentAllOrder(Long userId) {
        //查询当前用户的所有订单
        List<Order> list = orderMapper.selectByUserId(userId);
        return list;
    }

    @Override
    public List<OrderItem> getCurrentAllOrderItem(Long orderId) {
        //查询当前订单下的所有订单明细
        List<OrderItem> list = orderItemMapper.selectByOrderId(orderId);
        return list;
    }

    @Override
    public List<Order> getStatusOrder(Long userId, Integer status) {
        List<Order> list = orderMapper.getByStatus(userId, status);
        return list;
    }

    @Override
    public List<Map> getStatusOrders(Long userId, Integer status) {
        return orderMapper.getStatusOrders(userId,status);
    }

    @Override
    public  List<Map>  countOrder() {
        return orderMapper.countOrder(UserContext.getCurrentUser().getId());
    }

    @Override
    public Order getOrder(Long oid) {
        return orderMapper.selectByPrimaryKey(oid);
    }


    @Autowired
    private SignDetailMapper signDetailMapper;
    @Autowired
    private ShopCarItemMapper shopCarItemMapper;
    public void payOrder(Order order,Long [] pids) {
        List<ShopCarItem> shopCarItems = shopCarItemMapper.queryProductsByIds(pids,UserContext.getCurrentUser().getId());
        order.setUserId(UserContext.getCurrentUser().getId());
        order.setCreateTime(new Date());
        order.setOrderNumber(System.currentTimeMillis());
        order.setStatus(0);
        orderMapper.insert(order);
        Map<Long, ShopCarItem> currentUserShopCar = UserContext.getCurrentUserShopCar();
        for (ShopCarItem shopCarItem : shopCarItems) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(order.getId());
            orderItem.setCount(shopCarItem.getNumber());
            orderItem.setProductId(shopCarItem.getProductId());
            orderItem.setProductName(shopCarItem.getProductName());
            orderItem.setProductPrice(shopCarItem.getProductPrice());
            orderItem.setProductPic(shopCarItem.getProductPic());
           orderItemMapper.insert(orderItem);
           //删除购物车里 和session里的 购物项
           shopCarItemMapper.deleteItem(shopCarItem.getId());
           currentUserShopCar.remove(shopCarItem.getProductId());
        }
    }

    @Override
    public void changeOrderstatus(OrderQueryObject qo) {
        //如果是支付
        if(qo.getStatus()==Order.STATE_SHIPPED){
            orderMapper.updateStatus(qo);
            Order order = orderMapper.selectByPrimaryKey(qo.getOrderId());
            SignDetail signDetail = signDetailMapper.selectByPrimaryKey(UserContext.getCurrentUser().getId());
            //减积分
            Long grade = signDetail.getGrade() - order.getCost();
            signDetail.setGrade(grade);
            signDetailMapper.updateByPrimaryKey(signDetail);
        }else {
            orderMapper.updateStatus(qo);
        }
    }
}
